﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmNhaCungCap : Form
    {
        public frmNhaCungCap()
        {
            InitializeComponent();
        }

        private DataTable _dtbNhaCungCap = KetNoi.GetTable("select * from tblNhaCungCap");
        public int i = 0;
        private void frmNhaCungCap_Load(object sender, EventArgs e)
        {
            LoaddtgNhaCungCap();
        }

        public void LoaddtgNhaCungCap()
        {
            dtgNhaCC.DataSource = KetNoi.GetTable("select * from tblNhaCungCap");
            dtgNhaCC.AllowUserToAddRows = false;
            dtgNhaCC.AllowUserToDeleteRows = false;
            dtgNhaCC.Columns["MaNhaCC"].HeaderText = "Mã nhà cung cấp";
            dtgNhaCC.Columns["TenNhaCC"].HeaderText = "Tên nhà cung cấp";
            dtgNhaCC.Columns["DiaChi"].HeaderText = "Địa chỉ";
            dtgNhaCC.Columns["GhiChu"].HeaderText = "Ghi chú";
            dtgNhaCC.Columns["SoDienThoai"].HeaderText = "Số điện thoại";
            Lock();
        }

        public void Lock()
        {
            txtMaNCC.Enabled = false;
            txtTenNCC.Enabled = false;
            txtDiaChi.Enabled = false;
            txtGhiChu.Enabled = false;
            btnIn.Enabled = true;
            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnThoat.Enabled = true;
            btnXoa.Enabled = true;
            btnNew.Enabled = true;
        }
        public void unLock()
        {
            txtMaNCC.Enabled = true;
            txtTenNCC.Enabled = true;
            txtDiaChi.Enabled = true;
            txtGhiChu.Enabled = true;
            btnIn.Enabled = false;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnThoat.Enabled = true;
            btnXoa.Enabled = false;
            btnNew.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaNCC.Enabled == true)
            {
                DataRow newr = _dtbNhaCungCap.NewRow();
                newr["MaNhaCC"] = txtMaNCC.Text;
                newr["TenNhaCC"] = txtTenNCC.Text;
                newr["DiaChi"] = txtDiaChi.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                newr["SoDienThoai"] = txtSoDT.Text;
                i = KetNoi.ExcuteSQL("insert into tblNhaCungCap values('" + newr["MaNhaCC"] + "',N'"
                                                                          + newr["TenNhaCC"] + "',N'"
                                                                          + newr["DiaChi"] + "',N'"
                                                                          + newr["GhiChu"] + "',N'"
                                                                          + newr["SoDienThoai"] + "')");
                if (i > 0)
                {
                    MessageBox.Show("Lưu Thành Công !");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại !");
                }
                LoaddtgNhaCungCap();
            }
            else
            {
                DataRow newr = _dtbNhaCungCap.NewRow();
                newr["MaNhaCC"] = txtMaNCC.Text;
                newr["TenNhaCC"] = txtTenNCC.Text;
                newr["DiaChi"] = txtDiaChi.Text;
                newr["GhiChu"] = txtGhiChu.Text;
                newr["SoDienThoai"] = txtSoDT.Text;
                i = KetNoi.ExcuteSQL("update tblNhaCungCap set TenNhaCC=N'" + newr["TenNhaCC"]
                                                            + "',DiaChi=N'" + newr["DiaChi"]
                                                            + "',GhiChu=N'" + newr["GhiChu"]
                                                            + "',SoDienThoai=N'" + newr["SoDienThoai"]
                                                            + "' where MaNhaCC='" + newr["MaNhaCC"] + "'");
                if (i > 0)
                {
                    MessageBox.Show("Sửa thành công !");
                }
                else
                {
                    MessageBox.Show("Sửa thất bại");
                }
                LoaddtgNhaCungCap();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtMaNCC.Text = "";
            txtTenNCC.Text = "";
            txtDiaChi.Text = "";
            txtGhiChu.Text = "";
            txtSoDT.Text = "";
            unLock();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            unLock();
            txtMaNCC.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if ((txtMaNCC.Enabled == true) && (txtMaNCC == null))
            {
                MessageBox.Show("Chưa chọn nhà cung cấp");
            }
            else
            {
                i = KetNoi.ExcuteSQL("delete from tblNhaCungCap where MaNhaCC='" + txtMaNCC.Text + "'");

                if (i > 0)
                {
                    MessageBox.Show("Xóa thành công");
                }
                else
                {
                    MessageBox.Show("Xóa thất bại");
                }
                LoaddtgNhaCungCap();
            }
        }

        private void dtgNhaCC_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtMaNCC.Text = _dtbNhaCungCap.Rows[e.RowIndex]["MaNhaCC"].ToString();
                txtTenNCC.Text = _dtbNhaCungCap.Rows[e.RowIndex]["TenNhaCC"].ToString();
                txtDiaChi.Text = _dtbNhaCungCap.Rows[e.RowIndex]["DiaChi"].ToString();
                txtGhiChu.Text = _dtbNhaCungCap.Rows[e.RowIndex]["GhiChu"].ToString();
                txtSoDT.Text = _dtbNhaCungCap.Rows[e.RowIndex]["SoDienThoai"].ToString();
            }
            return;
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            dtgNhaCC.DataSource = KetNoi.GetTable("select * from tblNhaCungCap where TenNhaCC like'" + '%' + txtTimKiem.Text + "%" + "' or MaNhaCC like'" + '%' + txtTimKiem.Text + '%' + "'");
            dtgNhaCC.AllowUserToAddRows = false;
            dtgNhaCC.AllowUserToDeleteRows = false;
            dtgNhaCC.Columns["MaNhaCC"].HeaderText = "Mã nhà cung cấp";
            dtgNhaCC.Columns["TenNhaCC"].HeaderText = "Tên nhà cung cấp";
            dtgNhaCC.Columns["DiaChi"].HeaderText = "Địa chỉ";
            dtgNhaCC.Columns["GhiChu"].HeaderText = "Ghi chú";
            dtgNhaCC.Columns["SoDienThoai"].HeaderText = "Số điện thoại";
            Lock();
        }

        private void dtgNhaCC_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (byte j = 0; j < dtgNhaCC.Rows.Count; j++)
            {
                dtgNhaCC.Rows[j].DefaultCellStyle.BackColor = Color.Honeydew;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
